package com.code.generate;

import java.util.Map;

import com.code.generate.enums.FieldType;

/**
 * 
 * sql server查询结果生成的字段对象
 * 
 * @author Administrator
 * 
 */
public class SqlServerField extends Field {

	public SqlServerField(Map<String, Object> map) {

		this.tableField = ("" + map.get("COLUMN_NAME")).trim();
		this.dataType = ("" + map.get("DATA_TYPE")).trim();

		// this.fieldName = CommonUtils.getProperty(("" +
		// map.get("COLUMN_NAME")).trim());
		this.fieldName = CommonUtils.changeFirstLower(this.tableField);
		this.fieldType = CommonUtils.getFieldType(("" + map.get("DATA_TYPE")).trim(), this.tableField);

		if ("int".equals(this.dataType)) {
			this.dataType = "Integer";
			this.fieldType = FieldType.INTEGER;
		}

		if ("datetime".equals(this.dataType)) {
			this.dataType = "Timestamp";
			this.fieldType = FieldType.TIMESTAMP;
		}

		if ("date".equals(this.dataType)) {
			this.fieldType = FieldType.DATE;
		}

		if ("bit".equals(this.dataType)) {
			this.fieldType = FieldType.BOOLEAN;
		}

		if ("bigint".equals(this.dataType)) {
			this.fieldType = FieldType.LONG;
		}

		if (tableField.equalsIgnoreCase("AUDIT_STATE")) {
			this.fieldType = "AuditState";
			this.fieldName = "auditState";
		}
		if ("NO".equals(("" + map.get("IS_NULLABLE")).trim())) {
			this.whetherRequest = true;
		}
		if ("PRI".equals(("" + map.get("COLUMN_KEY")).trim())) {
			this.whetherPrimaryKey = true;
			this.whetherEdit = false;
			this.whetherList = false;
			this.isUnique = true;
		}
		if ("String".equals(fieldType)) {
			try {
				this.length = new Integer(("" + map.get("CHARACTER_MAXIMUM_LENGTH")).trim()).intValue();
			} catch (java.lang.NumberFormatException e) {
				this.length = 0;
			}

		}
		this.remark = ("" + map.get("COLUMN_COMMENT")).trim();

	}

	@Override
	public String getDataType() {
		return dataType.toUpperCase();
	}

}
